/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2012 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.huguesjohnson.retroleague.fb.PhotoItem;
import com.huguesjohnson.retroleague.util.HttpFetch;

public class AlbumImageAdapter extends BaseAdapter{
	private final static String TAG="AlbumImageAdapter";
	private ArrayList<PhotoItem> photos;
	private Context context;
	private int albumGalleryItemBackground;
	
	public AlbumImageAdapter(Context context,ArrayList<PhotoItem> photos){
		this.context=context;
		this.photos=photos;
        TypedArray ta=context.obtainStyledAttributes(R.styleable.AlbumGallery);
        albumGalleryItemBackground=ta.getResourceId(R.styleable.AlbumGallery_android_galleryItemBackground,0);
        ta.recycle();
	}
	
	@Override
	public int getCount(){
		return(this.photos.size());
	}

	@Override
	public Object getItem(int position){
		return(this.photos.get(position));
	}

	@Override
	public long getItemId(int position){
		return(position);
	}

	@Override
	public View getView(int position,View convertView,ViewGroup parent){
        ImageView imageView=new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(albumGalleryItemBackground);
        //load the image
        String pictureUrl=this.photos.get(position).getPictureUrl();
        try{
			Bitmap bitmap=ImageCache.get(pictureUrl);
			if(bitmap==null){
				bitmap=HttpFetch.fetchBitmap(pictureUrl);
				ImageCache.put(pictureUrl,bitmap);
			}
			imageView.setImageBitmap(bitmap);
        }catch(Exception x){
        	Log.e(TAG,"getView",x);
        }
        return(imageView);
	}
}